package com.buka.user.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.buka.server.pojo.Address;
import com.buka.user.mapper.AddressMapper;
import com.buka.user.service.AddressService;
import com.buka.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private TokenDecode tokenDecode;

    @Override
    public List<Address> findList() {
        String username = tokenDecode.getUserInfo().get("user_name");
        Example example = new Example(Address.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0);
        criteria.andEqualTo("userName",username);
        List<Address> addresses = addressMapper.selectByExample(example);
        return addresses;
    }

    @Override
    public void add(Address address) {
        String username = tokenDecode.getUserInfo().get("user_name");
        address.setId(IdWorker.getId());
        address.setUserName(username);
        addressMapper.insertSelective(address);
    }

    @Override
    public void update(Address address) {
        address.setUpdateTime(new  Date());
        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public void delete(Long id) {
        Address address = new Address();
        address.setId(id);
        address.setIsDelete(1);
        this.update(address);
    }
}
